﻿@{
    ViewBag.Title = "新增数据源配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
@section Head{}
<script type="text/javascript">
    var UrlArray = $.getUrlArray();
    var arr = new Array();
    var retData = new Array();
    var retDataOn = new Array();
    var update = false;
    var a = "";
    var opts = {
        height: 250,
        rows: 10,
        idField: "GUID",
        para: { modelID: UrlArray["modelID"], dbName: encodeURI(UrlArray["dbName"]) },
        columns: [
            { field: 'DisplayName', title: '表名称', width: 30, formatter: function (value, rowData, rowIndex) {
                return "<a href='#' onclick='OpenTableInfo(\"" + rowData.ViewName + "\",\"" + rowData.DisplayName.replace(/\"/g, "") + "\");' class='grid_a'>" + value + "</a>";
            }
            },
            { field: 'ViewChnName', title: '表说明', width: 30 },
            { field: 'SourceSysName', title: '源系统名称', width: 30 },
            { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                var add = true; ;
                for (var j = 0; j < retData.length; j++) {
                    if (retData[j].GUID == value) {
                        add = false;
                    }
                }
                if (add) {
                    retData.push(rowData);
                }
                return "<a href='#' onclick='remove(\"" + value + "\")'>删除</a>";
            }
            }
            ]
    };
    var optsOn = {
        height: 200,
        rows: 10,
        idField: "GUID",
        para: { modelID: UrlArray["modelID"], dbName: encodeURI(UrlArray["dbName"]) },
        columns: [
                { field: 'LeftTableName', title: '表名', width: 20 },
                { field: 'LeftColumnsName', title: '列名', width: 20 },
                { field: 'Operator', title: '操作符', width: 10, align: 'center' },
                { field: 'RightTableName', title: '表名', width: 20 },
                { field: 'RightColumnsName', title: '列名', width: 20 },
                { field: 'GUID', title: '操作', width: 10, align: 'center', formatter: function (value, rowData, rowIndex) {
                    var add = true; ;
                    for (var j = 0; j < retDataOn.length; j++) {
                        if (retDataOn[j].GUID == value) {
                            add = false;
                        }
                    }
                    if (add) {
                        retDataOn.push(rowData);
                    }
                    return "<a href='#' onclick='removeOn(\"" + value + "\")'>删除</a>";
                }
                }
               ]
    };
    var insert = false;
    $(function () {
        if (UrlArray["modelID"]) {
            opts.url = "/ODSConfig/GetStging_dbInfo";
            optsOn.url = "/ODSConfig/GetOnInfo";
        }
        $("#DataGrid").dg(opts);
        $('#DataGridOn').dg(optsOn);
        if (retData.length == 0)
            insert = true;
        $("#link a").each(function () {
            $(this).attr("href", $(this).attr("href") + "?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]));
        })
    });
    var saveType = "";
    function save(t) {
        $("#msg").html("");
        if (retData.length < 1) {
            alert("请选择源系统表！");
            return;
        }
        if (retData.length > 1 && retDataOn.length < 1) {
            alert("请添加整合条件！");
            return;
        }
        if (retData.length - retDataOn.length > 1) {
            alert("缺少必要整合条件！请添加整合条件！");
            return;
        }
        saveType = t; 
        saveDate();

    }
    function returnDate(v) {
        if (v == "1") {
            saveDate();
        }
    }
    function saveDate() {
        var odsid = UrlArray["modelID"];
        var dbName = UrlArray["dbName"];

        $("#loading").show();
        $.post("/ODSConfig/InsertItem", { odsid: odsid, stgingdbTables: $.toJSON(retData), onDataGridData: $.toJSON(retDataOn), nmID: UrlArray["nmID"], ndbName: encodeURI(UrlArray["ndbName"]), dbName: encodeURI(dbName) }, function (result) {
            if (result[0] == "1") {
                if (saveType == "next") {
                    window.location = "/DQConfig/Index?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
                }
                else
                    alert("操作成功！");
            }
            else if (result[0] == "0")
                alert("操作失败！");
            else
                $("#msg").html(result);

            $("#loading").hide();
        });
    }
    function back() {
        window.location = "/ModelConfig/Edit?modelID=" + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]) + "&nmID=" + UrlArray["nmID"] + "&ndbName=" + encodeURI(UrlArray["ndbName"]);
    } 
    function AddItem() {
        showModalMany('AddItem', '/ODSConfig/SourceList?modelID=' + UrlArray["modelID"] + "&dbName=" + encodeURI(UrlArray["dbName"]), '900', '480', returnFun);
    }
    function AddOn() {
        showModalMany('AddOn', '/ODSConfig/ODSConfigEdit?id=0&dbName=' + encodeURI(UrlArray["dbName"]), '600', '180', returnFunOn);
    }
    function OpenTableInfo(v, t) {
        showModalMany('Info', '/ODSConfig/TableInfo?tableName=' + v + '&tbName=' + t + '&dbName=' + encodeURI(UrlArray["dbName"]), '900', '480');
    }
    function remove(v) {
        if (confirm("确定要删除吗？")) {
            for (var j = 0; j < retData.length; j++) {
                if (retData[j].GUID == v) {
                    retData.removeAt(j);
                }
            }
            var data = { "total": 1, "rows": retData };
            opts["data"] = data;
            $("#DataGrid").dg(opts);
            update = true;
        }
    }
    function removeOn(v) {
        if (confirm("确定要删除吗？")) {
            for (var j = 0; j < retDataOn.length; j++) {
                if (retDataOn[j].GUID == v) {
                    retDataOn.removeAt(j);
                }
            }
            var data = { "total": 1, "rows": retDataOn };
            optsOn["data"] = data;
            $("#DataGridOn").dg(optsOn);
            update = true;
        }
    }
    function returnFun(v) {
        for (var i = 0; i < v.arr.length; i++) {
            var add = true;
            for (var j = 0; j < retData.length; j++) {
                if (retData[j].GUID == v.arr[i].GUID) {
                    add = false;
                }
            }
            if (add) {
                retData.push(v.arr[i]);
                update = true;
            }
        }
        var data = { "total": 1, "rows": retData };
        opts["data"] = data;
        $("#DataGrid").dg(opts);
    }
    function returnFunOn(v) {
        retDataOn.push(v);
        var data = { "total": 1, "rows": retDataOn };
        optsOn["data"] = data;
        $("#DataGridOn").dg(optsOn);
        update = true;
    }
</script>
<div style="margin: 5px;">
    <table class="searchArea" width="100%" style="padding-left: 5px; padding-right: 5px;
        margin-bottom: 5px;" cellpadding="0" cellspacing="0">
        <tr>
            <td valign="top" height="260">
                <table id="link" class="listTitle">
                    <tr>
                        <td width="15">
                            <img src="/images/edit.png" />
                        </td>
                        <td width="300">
                            <a href="/ModelConfig/Index">模型管理</a> --> <a href="/ModelConfig/Edit">新增模型配置</a>
                            -->选择源系统表
                        </td>
                        <td align="right">
                            <input type="button" class="button" value="添加" onclick="AddItem();" />
                        </td>
                    </tr>
                </table>
                <table id="DataGrid" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
        <tr>
            <td valign="top" height="260">
                <table class="listTitle">
                    <tr>
                        <td width="15">
                            <img src="/images/edit.png" />
                        </td>
                        <td width="300">
                            整合条件
                        </td>
                        <td align="right">
                            <input type="button" class="button" value="添加" onclick="AddOn();" />
                        </td>
                    </tr>
                </table>
                <table id="DataGridOn" class="pagTable" cellpadding="0" cellspacing="0">
                </table>
            </td>
        </tr>
    </table>
    <div id="loading" class="loading">
        <img src="/Images/tree_loading.gif" />
    </div>
    <div id="msg">
    </div>
    <input type="button" class="button" value="上一步" onclick="back()" />
    <input type="button" class="button" value="保存" onclick="save()" />
    <input type="button" class="button" value="下一步" onclick="save('next')" />
</div>
